package com.example.xViewChat.UI;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.example.xViewChat.Bean.IntentBean;
import com.example.xViewChat.Bean.testBean;
import com.example.xViewChat.NettyClient.PushManager;
import com.example.xViewChat.R;
import com.example.xViewChat.Service.PushService;
import com.example.xViewChat.Util.VerifyUtils;
import io.ganguo.chat.core.protocol.Commands;
import io.ganguo.chat.core.protocol.Handlers;
import io.ganguo.chat.core.transport.Header;
import io.ganguo.chat.core.transport.IMResponse;
import io.ganguo.chat.route.biz.bean.ClientType;
import io.ganguo.chat.route.biz.entity.User;
import io.ganguo.chat.route.server.dto.UserDTO;

import java.util.Random;

/**
 * Created by hasee on 2016-04-03.
 */
public class LoginAcitivity extends EventListenerActivity {
    private Context mContext;

    private Button mLoginButton;
    private EditText mAccount;
    private EditText mPassword;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        mContext = this;

        mLoginButton = (Button) findViewById(R.id.login);
        mAccount = (EditText) findViewById(R.id.account);
        mPassword = (EditText) findViewById(R.id.password);

        mAccount.setText("test1000");
        mPassword.setText("test1000");
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = mAccount.getText().toString().trim();
                String password = mPassword.getText().toString().trim();
                if (account.equals("")) {
                    showCustomToast("请填写帐号");
                    mAccount.requestFocus();
                } else if (password.equals("")) {
                    showCustomToast("请输入密码");
                } else if (!VerifyUtils.matchAccount(account)) {
                    showCustomToast("帐号不符合规范");
                    mAccount.requestFocus();
                } else if (mPassword.length() < 6) {
                    showCustomToast("密码长度需要大于6位");
                } else {
                    tryLogin(account, password);
                }
            }
        });

    }

    private void tryLogin(String account, String password) {
        User user = new User();
        user.setClientType(ClientType.MAC.value());

        user.setAccount(account);
        user.setPassword(password);

        IMResponse resp = new IMResponse();
        Header header = new Header();
        header.setHandlerId(Handlers.USER);
        header.setCommandId(Commands.LOGIN_REQUEST);

        IntentBean intentBean = new IntentBean();
        intentBean.setHeader(header);
        intentBean.setEntity(user);

        PushManager.send(this, intentBean);
    }

    @Override
    public void onReplyReceived(IntentBean intentBean) {
        switch (intentBean.getHeader().getCommandId()) {
            case Commands.LOGIN_SUCCESS:
                System.out.println("来到了onConnectionSucceed，将要启动loginActivity");
                Intent intent = new Intent(LoginAcitivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case Commands.LOGIN_CHANNEL_FAIL:
                showCustomToast("登录失败，请验证一下账号问题。");
                break;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        PushManager.destory(this);
    }


}
